package cn.how2go.net.server.thread;

import cn.how2go.net.server.base.SessionData;
import cn.how2go.net.server.bean.ByteMessage;
import cn.how2go.net.server.protocol.JTBytes;
import cn.how2go.net.server.protocol.MessageType;
import cn.how2go.net.server.service.ProtocolService;
import cn.how2go.net.server.service.SenderService;
import cn.how2go.net.server.vo.CtxMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CtxMessageRunnable implements Runnable {
    private Logger logger = LoggerFactory.getLogger(CtxMessageRunnable.class);

    private SenderService senderService;

    public CtxMessageRunnable(SenderService senderService) {
        this.senderService = senderService;
    }

    @Override
    public void run() {
        while(true) {
            CtxMessage ctxMessage = null;
            if (SessionData.RECEIVE_MESSAGE.size() < 1){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    logger.error("CtxMessageRunnable Sleep Error>>>>" + e.getMessage());
                }
            } else {
                ctxMessage = SessionData.RECEIVE_MESSAGE.poll();
                byte[] response = decoderMessage(senderService,ctxMessage.getMessage());
                if(response != null && ctxMessage.getCtx()!=null) {
                    try{
                        ctxMessage.getCtx().channel().writeAndFlush(response);
                        //ByteMessageService.write(new ByteMessage("S",response));
                        SessionData.BYTEMESSAGE.offer(new ByteMessage("S",response));
                    }catch (Exception e){
                        //发送报文失败，删除无效ctx;
                        JTBytes jtBytes = new JTBytes();
                        jtBytes.read(response);
                        SessionData.AllTERMINAL.remove(jtBytes.getHeader().getSimno());
                        logger.error("ctx<<<<<<发送失败");
                    }
                }
            }
        }
    }

    private byte[] decoderMessage(SenderService senderService,JTBytes jtBytes){
        //回复的字节报文
        byte[] response = null;
        //获取消息ID的字节数组
        short msgType = jtBytes.getHeader().getMsgType();
        //消息类型转换
        MessageType type = MessageType.getByValue(msgType);
        try {
            switch (type) {
                case JT_0001:
                    response = ProtocolService.protocolJT0001(jtBytes);
                    break;
                case JT_0002:
                    response = ProtocolService.createJT8001(jtBytes,(byte)0x00);
                    break;
                case JT_0003:
                    break;
                case JT_0100:
                    response = ProtocolService.protocolJT0100(jtBytes);
                    break;
                case JT_0102:
                    response = ProtocolService.protocolJT0102(jtBytes);
                    break;
                case JT_0200:
                    response = ProtocolService.protocolJT0200(senderService,jtBytes);
                    break;
                case JT_0704:
                    response = ProtocolService.protocolJT0704(jtBytes);
                    break;
                default:
            }
        }catch (Exception e){
            logger.error("error msg<<<<<<<<<<"+e.getMessage());
            //解析出错的消息保存到日志中
            logger.info(jtBytes.toString());
            response = ProtocolService.createJT8001(jtBytes,(byte) 0x01);
        }
        return response;
    }
}
